package org.ccay.excel.importer.validate;

import org.ccay.core.util.StringUtil;
import org.ccay.excel.importer.IExcelImportContext;
import org.ccay.excel.importer.ITitleMapper;
import org.ccay.excel.importer.RowData;


/**
 * IValidator的抽象实现。
 * 	
 * @author chaowangbang
 * @since Mar 26, 2012
 */
public abstract class AbstractValidator implements IValidator {
	
	private String type;
	
	/** 消息的I18N码 **/
	private String messageKey;
	
	/** 默认的消息 **/
	private String defaultMessage;
	
	private IValidationContext context;
	
	/** Excel导入时的上下文信息 **/
	private IExcelImportContext importContext;
	
	/** 待校验的单元格值 **/
	//private Object value;

	/* (non-Javadoc)
	 * @see org.ccay.excel.importer.validate.IValidator#setType(java.lang.String)
	 */
	public void setType(String type) {
		this.type = type;
	}

	/* (non-Javadoc)
	 * @see org.ccay.excel.importer.validate.IValidator#getType()
	 */
	public String getType() {
		return this.type;
	}

	/* (non-Javadoc)
	 * @see org.ccay.excel.importer.validate.IValidator#setMessageKey(java.lang.String)
	 */
	public void setMessageKey(String messageKey) {
		this.messageKey = messageKey;
	}
	
	/* (non-Javadoc)
	 * @see org.ccay.excel.importer.validate.IValidator#getMessageKey()
	 */
	public String getMessageKey() {
		return this.messageKey;
	}
	
	public final IStatus validate(Object value) {
		//this.value = value;
		return doValidate(value);
	}
	
	protected abstract IStatus doValidate(Object value);

	/** 
	 * 设置默认的消息。
	 * 	
	 * @author chaowangbang
	 * @since Mar 27, 2012
	 * @param defaultMessage
	 */
	public void setDefaultMessage(String defaultMessage) {
		this.defaultMessage = defaultMessage;
	}
	
	/* (non-Javadoc)
	 * @see org.ccay.excel.importer.validate.IValidator#getValidationContext()
	 */
	public IValidationContext getValidationContext() {
		return this.context;
	}
	
	/* (non-Javadoc)
	 * @see org.ccay.excel.importer.validate.IValidator#setValidationContext(org.ccay.excel.importer.validate.IValidationContext)
	 */
	public void setValidationContext(IValidationContext context) {
		this.context = context;
	}
	
	/* (non-Javadoc)
	 * @see org.ccay.excel.importer.validate.IValidator#getExcelImportContext()
	 */
	public IExcelImportContext getExcelImportContext() {
		return this.importContext;
	}

	/* (non-Javadoc)
	 * @see org.ccay.excel.importer.validate.IValidator#setExcelImportContext(org.ccay.excel.importer.IExcelImportContext)
	 */
	public void setExcelImportContext(IExcelImportContext importContext) {
		this.importContext = importContext;
	}

	/* (non-Javadoc)
	 * @see org.ccay.excel.importer.validate.IValidator#isShortCircuit()
	 */
	public boolean isShortCircuit() {
		return true; //默认是短路操作，子类可以重载它
	}

	/** 创建一个error Status	
	 * @author chaowangbang
	 * @since Mar 26, 2012
	 * @param args
	 * @return
	 */
	protected IStatus error() {
		String message = this.context.getText(messageKey, defaultMessage, this);
		return Status.error(message);
	}
	
	/**
	 * 获取对应标题的value
	 * @param title
	 * @return
	 * Created：2012-7-12
	 * @author chaowangbang
	 */
	@Deprecated
	public String getTitleValue(String title){
		RowData  roleData = getValidationContext().getCurrentRowDatas();
		ITitleMapper titleMapper = getValidationContext().getTitleMapper();
		if(!StringUtil.isNullOrEmpty(title)){
			String field = titleMapper.getField(title);
			String colIndex = titleMapper.getCol(field);
			Object value = roleData.getColDataByColIndex(colIndex);
			if(null != value && !StringUtil.isNullOrEmpty(value.toString())){
				return  value.toString();
			}
		}
		return "";
	}
	/**
	 * 获取对应标题的value
	 * @param title
	 * @return
	 * Created：2012-7-12
	 * @author chaowangbang
	 */
	@Deprecated
	public Object getTitleObjectValue(String title){
		RowData  roleData = getValidationContext().getCurrentRowDatas();
		ITitleMapper titleMapper = getValidationContext().getTitleMapper();
		if(!StringUtil.isNullOrEmpty(title)){
			String field = titleMapper.getField(title);
			String colIndex = titleMapper.getCol(field);
			return  roleData.getColDataByColIndex(colIndex);
		}
		return null;
	}
	
	/**
	 * 获取对应Field的value
	 * @param fieldName
	 * @return
	 * Created：2014-01-12
	 * @author chaowangbang
	 */
	public Object getFieldNameObjectValue(String fieldName){
		RowData  roleData = getValidationContext().getCurrentRowDatas();
		ITitleMapper titleMapper = getValidationContext().getTitleMapper();
		if(!StringUtil.isNullOrEmpty(fieldName)){
			String colIndex = titleMapper.getCol(fieldName);
			return  roleData.getColDataByColIndex(colIndex);
		}
		return null;
	}

}
